﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace DSClient
{
    public partial class frmClient : Form
    {
        static TcpClient socket;
        static StreamReader sr;
        static StreamWriter sw;
        static int port = 17112;
        static bool connected = false;
        static bool listening = false;
        static string username;

        public frmClient()
        {
            InitializeComponent();
        }
        private void frmClient_Load(object sender, EventArgs e)
        {
            
        }
        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtInput.Text.Length > 0 && e.KeyChar == 13)
            {
                DoCommand(txtInput.Text);
                txtInput.Clear();
                txtInput.Focus();

                e.Handled = true;
            }
        }
        private void menuFileExit_Click(object sender, EventArgs e)
        {
            DoCommand("exit");
        }
        private void frmClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            DoCommand("exit");
        }
        private void menuFileDisconnect_Click(object sender, EventArgs e)
        {
            DoCommand("disconnect");
        }  
        private void menuFileConnect_Click(object sender, EventArgs e)
        {
            CConnectionInfo cinfo = new CConnectionInfo();
            frmConnect form = new frmConnect(cinfo);
            form.ShowDialog();

            DoCommand("connect " + cinfo.address + " " + cinfo.username + " " + cinfo.password);
        }

        private void Send(string msg)
        {
            if (connected)
            {
                try
                {
                    sw.WriteLine(msg);
                    sw.Flush();
                }
                catch (Exception e)
                {
                    OutputWriteLine("ERROR (Send): " + e.Message);
                }
            }
        }
        private void ReceiveThread()
        {
            while (listening)
            {
                try
                {
                    string msg = sr.ReadLine();
                    if (msg != null)
                    {
                        //OutputWriteLine("<< " + msg);
                        HandleMessage(msg);
                    }
                }
                catch (Exception e)
                {
                    OutputWriteLine("ERROR (ReceiveThread): " + e.Message);
                }

                Application.DoEvents();
            }
        }

        private delegate void SetTextCallback(string output);
        private void OutputWrite(string output)
        {
            try     // Throws an exception when exiting
            {
                if (txtOutput.InvokeRequired)
                {
                    SetTextCallback callback = new SetTextCallback(OutputWriteLine);
                    txtOutput.Invoke(callback, output);
                }
                else
                {
                    txtOutput.AppendText(output);
                }
            }
            catch (Exception e)
            {
            }
        }
        private void OutputWriteLine(string output)
        {
            OutputWrite(output.TrimEnd('\r','\n') + Environment.NewLine);
        }

        private void Connected()
        {
            if (!connected)
            {
                connected = true;
                //listening = true;

                OutputWriteLine("Connected as " + username + ".");
            }
        }
        private void Disconnect()
        {
            if (connected)
            {
                Send("{DISCONNECT}");

                connected = false;
                listening = false;

                socket.Close();

                OutputWriteLine("Disconnected.");
            }
        }
    }
}
